var index=0;
$(function () {
    index=$("#index").val();
});


var specs=[]

$("#btn_append").click(function () {

    if(specs.length==0){
        var submit_url=$("#form1").attr("project_path")+"/goods/listForSpecs"
        $.ajax({
            type: "get",
            url: submit_url,
            async: false,
            success: function(datas){
                specs=datas;
            }
        });
    };

    var content = '<div class="form-group row">'+
    '<label class="col-sm-1 text-left control-label col-form-label">商品规格</label>'+
    '<div class="col-md-3">'+
    '<select onchange="change(this)" name="specIds" index="'+index+'" class="select2 form-control custom-select" style="width: 100%; height:36px;">'+
    '<option value="0">请选择商品规格</option>';
    
    $(specs).each(function (index,item) {
        content+= '<option value="'+item.id+'" >'+item.specName+'</option>';
    })
    
    content+= '</select>'+
    '</div>'+
    '<div class="col-md-3">'+
    '<select name="specvIds" id="specvs_'+index+'" class="select2 form-control custom-select" style="width: 100%; height:36px;">'+
    '<option value="0">请选择商品规格内容</option>'+
    '</select>'+
    '</div>'+
    '<button type="button" onclick="remove(this)" index="-1" class="btn btn-outline-primary">删除</button>'+
    '</div>';

    $("#gss").append(content);

    index++;
})

function remove(element) {
    if($(element).attr("index")!=-1){
        var id=$(element).attr("id");
        var submit_url=$("#form1").attr("project_path")+"/goods/removeForGoogsSpec?id="+id;
        $.get(submit_url,function (result) {
            if(!result)
                alert('删除商品规格失败！')
        })
    }
    $(element).parent().remove();
}


function  change(element) {
    var specId=$(element).val();
    var submit_url=$("#form1").attr("project_path")+"/goods/listBySpecId?specId="+specId;

    $.get(submit_url,function (specvs) {
        var id='specvs_'+$(element).attr("index");
        var content='<option value="0">请选择商品规格内容</option>';
        
        $(specvs).each(function (index,item) {
            content+='<option value="'+item.id+'">'+item.specvName+'</option>';
        })

        $("#"+id).empty().html(content);
    })
}
